package com.lovo.sheyingback.controller;

import com.lovo.sheyingback.bean.Role;
import com.lovo.sheyingback.bean.SystemDict;
import com.lovo.sheyingback.bean.User;
import com.lovo.sheyingback.bean.form.RoleForm;
import com.lovo.sheyingback.bean.form.UserForm;
import com.lovo.sheyingback.result.ResponseResult;
import com.lovo.sheyingback.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/roles")
@ResponseResult
@Tag(name = "角色控制器RoleController")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @GetMapping
    @Operation(summary = "显示所有角色")
    public Object listRole(UserForm userForm){
        return roleService.listRole(userForm.getCurrent(),userForm.getPageSize());
    }
    @GetMapping("/functions/{id}")
    @Operation(summary = "查询已分配和所有权限")
    public Object listRoleFunction(@PathVariable Integer id){
        return roleService.listRoleFunction(id);
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID查找角色")
    public Object getById(@PathVariable Integer id){
        return roleService.getById(id);
    }

    @PostMapping
    @Operation(summary = "增加角色")
    public Object save(@RequestBody @Valid RoleForm roleForm){
        Role role = new Role();
        BeanUtils.copyProperties(roleForm,role);
        role.setCreateDate(LocalDateTime.now());
        role.setUpdateDate(LocalDateTime.now());
        return roleService.insert(role);

    }
    @PutMapping("/{id}")
    @Operation(summary = "修改角色")
    public Object update(@PathVariable Integer id,@RequestBody RoleForm roleForm){
        Role role = new Role();
        role.setId(id);
        BeanUtils.copyProperties(roleForm,role);
        return roleService.update(role);
    }
    @PutMapping("/assignFunction/{id}")
    @Operation(summary = "给角色分配权限")
    public Object assignRoleFunction(@PathVariable Integer id,@RequestBody RoleForm roleForm){
        return roleService.assignRoleFunction(id,roleForm.getFunctionIds());
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除角色")
    public Object delete(@PathVariable Integer id){
        return roleService.delete(id);
    }
}
